package com.shujia;

import java.sql.*;

public class MysqlJdbc {
    public static void main(String[] args) {

        Connection connection = null;
        Statement stat = null;
        try {


            //1、加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://192.168.129.101:3306/student";

            //2、创建连接
            connection = DriverManager.getConnection(url, "root", "123456");

            /**
             *统计每个班级学生的人数
             *
             */

            String sql = "select clazz,count(1) as c from student group by clazz";


            /**
             * 获取执行器
             *
             */
            stat = connection.createStatement();

            //执行查询
            ResultSet resultSet = stat.executeQuery(sql);

            //取出数据
            while (resultSet.next()) {
                String clazz = resultSet.getString("clazz");
                long c = resultSet.getLong("c");
                System.out.println(clazz + "\t" + c);
            }

        } catch (Exception e) {

        } finally {

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (stat != null) {
                try {
                    stat.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}
